<?php

class MembersController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $user = new Zend_Session_NameSpace("user");
		if((isset($user->username)&&$user->level==1)||((isset($user->username)&&$user->level==2))){
                    if ($user->level==1) {
                        
                    
                        $tinModel=new Model_Tin;
                        $tin=$tinModel->getAll();
                        $pa = Zend_Paginator::factory($tin);
                        $pa->setItemCountPerPage(6);
                        $pa->setPageRange(3);
                        $curent = $this->_request->getParam("page");
                        $pa->setCurrentPageNumber($curent);
                        $this->view->tin = $pa;
                    }  else {
                        $tinModel=new Model_Tin;
                        $tin=$tinModel->getAlluser($user->iduser);
                        $pa = Zend_Paginator::factory($tin);
                        $pa->setItemCountPerPage(6);
                        $pa->setPageRange(3);
                        $curent = $this->_request->getParam("page");
                        $pa->setCurrentPageNumber($curent);
                        $this->view->tin = $pa;
                    }
                    
                }  else {
                    $this->_redirect("/index");
                }
    }

    public function createnewAction()
    {
        // action body
        $user=new Zend_Session_Namespace("user");
        $tin= new Zend_Session_Namespace("tin");
        if((isset($user->username)&&$user->level==1)||((isset($user->username)&&$user->level==2)))
        {
                $form = new Form_Dangtin;
		if ($this->_request->isPost()) 
		{
                     
			if ($form->isValid($this->_request->getParams()))
			{
				// kiem tra dung du lieu
				
				$tin=new Model_Tin;
				
				$upload = new Zend_File_Transfer_Adapter_Http();

				//$upload->setDestination("./public/images");
				$files = $upload->getFileInfo();
				foreach ($files as $file =>$info){
					if($upload->isValid($file)){
						$upload->receive($file);
					}
				}
				$data=array(
                                                        
                                                        "iduser"=>$user->iduser,
							"idtl"=>$this->_request->getParam("idtl"),
                                                        "idlt"=>$this->_request->getParam("idlt"),
							"tieude"=>$this->_request->getPost("tieude"),
							"tomtat"=>$this->_request->getPost("tomtat"),
							"noidungtin"=>$this->_request->getPost("noidungtin")
                                                        
							
						);
				if($tin->insertTin($data))
					
                                    $this->_redirect("/index");
				else
					
                                    echo 'loi';
                                }
			else
			{
				// kiem tra sai du lieu
				$form->populate($this->_request->getParams());
			}
		}  else {
                    $this->view->form =  $form;
                }
         }
         else{
             echo 'ko id';
         }
         
    }

    public function updatenewAction()
    {
        // action body
    }

    public function deletenewAction()
    {
        // action body
    }

    public function locknewAction()
    {
        // action body
        $tinModel=new Model_User;
        $getidtin=$this->_request->getParam("idtin");
        $setdata=array('anhien'=>'0');
        $kq=$userModel->setUnlock($setdata, $getidtin);
        if ($kq==true) {
            $this->_redirect("/members/index");
        }  else {
            echo "lỗi";
        }
    }

    public function unlocknewAction()
    {
        // action body
        $tinModel=new Model_User;
        $getidtin=$this->_request->getParam("idtin");
        $setdata=array('anhien'=>'1');
        $kq=$userModel->setUnlock($setdata, $getidtin);
        if ($kq==true) {
            $this->_redirect("/menbers/index");
        }  else {
            echo "lỗi";
        }
    }


}











